package com.fanghuaiming.config.datasource;

import com.fanghuaiming.config.model.XaOrderDataSource;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * @author fanghuaiming
 */
@Configuration
@MapperScan(basePackages = OrderDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "orderSqlSessionFactory",sqlSessionTemplateRef = "orderSqlSessionTemplate")
public class OrderDataSourceConfig {

    static final String PACKAGE = "com.fanghuaiming.dao.mapper.order";

    @Autowired
    private XaOrderDataSource xaOrderDataSource;

    /**
     * @Description: 创建dataSource
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:09
     * @version:1.0.0
     */
    @SuppressWarnings("all")
    @Bean(name = "orderDataSource")
    @Primary
    public DataSource orderDataSource() {
        /*MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(xaOrderDataSource.getJdbcUrl());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXADataSource.setUser(xaOrderDataSource.getUsername());
        mysqlXADataSource.setPassword(xaOrderDataSource.getPassword());

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("orderDataSource");
        atomikosDataSourceBean.setMinPoolSize(xaOrderDataSource.getMinIdle());
        return mysqlXADataSource;*/

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("orderDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
        Properties properties = new Properties();
        properties.put("URL", xaOrderDataSource.getJdbcUrl());
        properties.put("user", xaOrderDataSource.getUsername());
        properties.put("password", xaOrderDataSource.getPassword());
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;

    }

    /**
     * @Description: 创建transactionManager
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:09
     * @version:1.0.0
     */
    /*@Bean(name = "orderTransactionManager")
    public DataSourceTransactionManager orderTransactionManager() {
        return new DataSourceTransactionManager(this.orderDataSource());
    }*/

    /**
     * @Description: 创建SqlSessionTemplate
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:09
     * @version:1.0.0
     */
    @Bean(name = "orderSqlSessionTemplate")
    public SqlSessionTemplate orderSqlSessionTemplate(@Qualifier("orderSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * @Description: 创建orderSqlSessionFactory
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:10
     * @version:1.0.0
     */
    @Bean(name = "orderSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("orderDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/order/*.xml"));
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }

}
